﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ns
{
    public class BinarySearchSolution : MonoBehaviour
    {
        public int MySqrt(int x)
        {
            if (x == 0) return 0;
            int left = 0, right = x;
            int ans = 0;

            while (left <= right)
            {
                int mid = left + (right - left) / 2;
                var sqr = System.Math.Pow(mid, 2); //溢出？？ mid*mid

                if (sqr <= x) {  ans = mid; left = mid + 1; }
                else if (sqr > x) { right = mid - 1; }
            }
            return ans;
        }
    }
}